package com.xgc.book.bookserver.util.shiro;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author PangJunjie
 * @Date 2023/11/21/021
 */
@Configuration
public class ShiroConfig {

    @Bean
    public SecurityManager securityManager(Shiro2Realm realm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(realm);
        securityManager.setRememberMeManager(null);
        return securityManager;
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);
        Map<String, Filter> filterMap = new HashMap<>();
        /*设置过滤器的名称，用来做登录授权访问，匿名访问的名称固定为：anon*/
        filterMap.put("admin", new Shiro2Filter());
        shiroFilter.setFilters(filterMap);

        Map<String, String> filters = new HashMap<>();
        filters.put("/kaptcha.jpg", "anon");
        filters.put("/login", "anon");
        filters.put("/static/**", "anon");
        filters.put("/api/v1/**", "anon");
        filters.put("/wx/v1/**", "anon");
        filters.put("/**", "admin");
        shiroFilter.setFilterChainDefinitionMap(filters);
        return shiroFilter;
    }
}
